package com.example.demo.nuoqi.controller;

import com.example.demo.nuoqi.dao.UserMapper;
import com.example.demo.nuoqi.pojo.Admin;
import com.example.demo.nuoqi.pojo.User;
import com.example.demo.nuoqi.service.UserService;
import com.example.demo.nuoqi.utils.AdminUtils;
import com.example.demo.nuoqi.utils.Result;
import com.example.demo.nuoqi.utils.StatusCode;
import com.example.demo.nuoqi.utils.UserUtils;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.*;
import javafx.scene.chart.ValueAxis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/****
 * @Author:shenkunlin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/
@Api(value = "UserController")
@RestController
@RequestMapping("/user")
@CrossOrigin(value = "*")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;

    /***
     * User分页条件搜索实现
     * @param user
     * @param page
     * @param size
     * @return
     */
    @ApiOperation(value = "User条件分页查询", notes = "分页条件查询User方法详情", tags = {"UserController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@RequestBody(required = false) @ApiParam(name = "User对象", value = "传入JSON数据", required = false) User user, @PathVariable int page, @PathVariable int size) {
        //调用UserService实现分页条件查询User
        PageInfo<User> pageInfo = userService.findPage(user, page, size);
        return new Result(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /***
     * User分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @ApiOperation(value = "User分页查询", notes = "分页查询User方法详情", tags = {"UserController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@PathVariable int page, @PathVariable int size) {
        //调用UserService实现分页查询User
        PageInfo<User> pageInfo = userService.findPage(page, size);
        return new Result<PageInfo>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /***
     *
     * @param user
     * @return
     */
    @ApiOperation(value = "User条件查询", notes = "条件查询User方法详情", tags = {"UserController"})
    @PostMapping(value = "/search")
    public Result<List<User>> findList(@RequestBody(required = false) @ApiParam(name = "User对象", value = "传入JSON数据", required = false) User user) {
        //调用UserService实现条件查询User
        List<User> list = userService.findList(user);
        return new Result<List<User>>(true, StatusCode.OK, "查询成功", list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @ApiOperation(value = "User根据ID删除", notes = "根据ID删除User方法详情", tags = {"UserController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable Integer id) {
        //调用UserService实现根据主键删除
        userService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /***
     * 修改User数据
     * @param user
     * @param id
     * @return
     */
    @ApiOperation(value = "User根据ID修改", notes = "根据ID修改User方法详情", tags = {"UserController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @PutMapping(value = "/{id}")
    public Result update(@RequestBody @ApiParam(name = "User对象", value = "传入JSON数据", required = false) User user, @PathVariable Integer id) {
        //设置主键值
        user.setId(id);
        //调用UserService实现修改User
        userService.update(user);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /***
     * 新增User数据
     * @param user
     * @return
     */
    @ApiOperation(value = "User添加", notes = "添加User方法详情", tags = {"UserController"})
    @PostMapping
    public Result add(@ApiParam(name = "User对象", value = "传入JSON数据", required = true) User user, @RequestParam(value = "file", required = false) MultipartFile file) {
        //调用UserService实现添加User
        user.setCreateTime(new Date());
        userService.add(user);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    /***
     * 根据ID查询User数据
     * @param id
     * @return
     */
    @ApiOperation(value = "User根据ID查询", notes = "根据ID查询User方法详情", tags = {"UserController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @GetMapping("/{id}")
    public Result<User> findById(@PathVariable Integer id) {
        //调用UserService实现根据主键查询User
        User user = userService.findById(id);
        return new Result<User>(true, StatusCode.OK, "查询成功", user);
    }

    /***
     * 查询User全部数据
     * @return
     */
    @ApiOperation(value = "查询所有User", notes = "查询所User有方法详情", tags = {"UserController"})
    @GetMapping
    public Result<List<User>> findAll() {
        //调用UserService实现查询所有User
        List<User> list = userService.findAll();
        return new Result<List<User>>(true, StatusCode.OK, "查询成功", list);
    }


    @GetMapping("/logout")
    @ApiOperation(value = "用户退出", notes = "用户退出", tags = {"UserController"})
    public Result logout(HttpSession session) {
        session.removeAttribute("user");
        return new Result(true, StatusCode.OK, "用户退出成功", null);
    }


    @PostMapping("/login")
    @ApiOperation(value = "用户登录", notes = "用户登录", tags = {"UserController"})
    public Result login(@RequestBody User user, HttpSession session) {
        User one = userMapper.selectOne(user);
        if (null != one && (one.getName().equals(user.getName()) && one.getPassword().equals(user.getPassword())) && !"禁用".equals(one.getStatus())) {
            session.setAttribute("user", one);
            return new Result(true, StatusCode.OK, "登录成功", user);
        } else {
            return new Result(false, StatusCode.ERROR, "登录失败", null);
        }
    }

    @GetMapping("/getUser")
    @ApiOperation(value = "获取用户信息", notes = "获取用户信息", tags = {"UserController"})
    public Result getUser(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (null != user) {
            return new Result(true, StatusCode.OK, "获取用户成功", user);
        } else {
            return new Result(true, StatusCode.ERROR, "获取用户失败", null);
        }
    }

    @GetMapping("/setUserStatus/{userId}/{result}")
    @ApiOperation(value = "设置用户状态 0启用 1禁用", notes = "设置用户状态 0启用 1禁用", tags = {"UserController"})
    public Result setUserStatus(@PathVariable("userId")Integer userId,@PathVariable("result") String result) {
        User byId = userService.findById(userId);
        if ("1" == result) {
            byId.setStatus("禁用");
        }else {
            byId.setStatus("启用");
        }
        return new Result(true, StatusCode.OK, "设置用户状态成功", null);
    }


    @PostMapping("/setNewPassword/{olPwd}/{newPwd}")
    @ApiOperation(value = "设置新密码", notes = "设置新密码", tags = {"AdminController"})
    public Result setNewPassword(@PathVariable("olPwd")String olPwd,@PathVariable("newPwd") String newPwd , HttpSession session) {
        User user = UserUtils.getUser(session);
        if (olPwd.equals(user.getPassword())) {
            User id = userService.findById(user.getId());
            id.setPassword(newPwd);
            userService.update(id);
            return new Result(true, StatusCode.OK, "设置新密码成功", null);
        }else{
            return new Result(false, StatusCode.ERROR, "您输入的旧密码有误", null);
        }
    }


    @GetMapping("/resetPwd/{userId}")
    @ApiOperation(value = "重置密码", notes = "重置密码", tags = {"AdminController"})
    public Result resetPwd(@PathVariable("userId")Integer userId ,HttpSession session) {
        User user = UserUtils.getUser(session);
        User id = userService.findById(userId);
        id.setPassword("123456");
        userService.update(id);
        return new Result(true, StatusCode.OK, "重置密码成功,密码为123456", null);
    }

}
